package org.chuan.org.service.impl;

import org.chuan.base.service.impl.BaseServiceImpl;
import org.chuan.org.domain.Department;
import org.chuan.org.mapper.DepartmentMapper;
import org.chuan.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public void save(Department department) {
        // 维护 path路径
        departmentMapper.save(department);
        department.setCreate_time(new Date());
        if (department.getParent() == null) {
            department.setPath("/"+department.getId());
        }else {
            Department parent = departmentMapper.queryById(department.getParent().getId());
            department.setPath(parent.getPath()+"/"+department.getId());
        }
        departmentMapper.update(department);
    }

    @Override
    public void update(Department department) {
        department.setUpdate_time(new Date());
        if (department.getParent() == null) {
            department.setPath("/"+department.getId());
        }else {
            Department parent = departmentMapper.queryById(department.getParent().getId());
            department.setPath(parent.getPath()+"/"+department.getId());
        }
        departmentMapper.update(department);
    }

    @Override
    public List<Department> queryDeptTree() {
        return departmentMapper.queryDeptTree();
    }


}
